@extends('admin.base')

@section('content')
    <style>
        /*.layui-table-cell {*/
        /*    height: 100%;*/
        /*    max-width: 100%;*/
        /*}*/

    </style>
    <style type="text/css">
        .layui-table-cell {
            height: auto;
            line-height: 20px;

        }
        .layui-table-cell img {
            height: 50%;
            max-width: 50%;
        }
    </style>
    <style type="text/css">
        /*.layui-table>tbody>tr {*/
        /*    height: 100px;*/
        /*}*/
        /*.layui-table tbody .laytable-cell-1-main_image {*/
        /*    height: 80px;*/
        /*}*/
    </style>
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-btn-group">
                <a class="layui-btn layui-btn-sm" href="{{ route('admin.goods_category_sku.create') }}">添 加</a>
                <button class="layui-btn layui-btn-sm" id="searchBtn">搜 索</button>
            </div>

            <div class="layui-form" >
                <div class="layui-input-inline">
                    <select name="category_id" lay-verify="required" lay-search id="category_id">
                        <option value="">请选择分类</option>
                        @foreach($categorys as $category)
                            <option value="{{ $category["id"] }}" >{{ $category["category_name"] }}</option>
                            @if(isset($category["_child"])&&$category["_child"])
                                @foreach($category["_child"] as $child)
                                    <option value="{{ $child["id"] }}" >┗━━{{ $child["category_name"] }}</option>
                                    @if(isset($child["_child"])&&!$child["_child"])
                                        @foreach($child["_child"] as $third)
                                            <option value="{{ $third["id"] }}" >┗━━{{ $third["category_name"] }}</option>
                                        @endforeach
                                    @endif
                                @endforeach
                            @endif
                        @endforeach
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>

            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
                </div>
            </script>

            <script type="text/html" id="thumb">
                <a href="@{{d.image}}" target="_blank" title="点击查看"><img src="@{{d.image}}" alt=""></a>
            </script>

        </div>
    </div>
@endsection

@section('script')
    <script>
        layui.use(['layer','table','form'],function () {
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            //用户表格初始化
            var dataTable = table.render({
                elem: '#dataTable'
                ,height: 500
                ,url: "{{ route('admin.goods_category_sku.data') }}" //数据接口
                ,page: true //开启分页
                ,cols: [[ //表头
                    {checkbox: true,fixed: true}
                    ,{field: 'id', title: 'ID', sort: true,width:80}
                    ,{field: 'category_name', title: '分类名称'}
                    ,{field: 'sku_name', title: '属性名称'}
                    ,{field: 'image', title: '封面图',toolbar:'#thumb',width:140}
                    ,{field: 'content', title: '属性值'}
                    ,{field: 'sort', title: '排序',width:100}
                    ,{field: 'created_at', title: '创建时间',width:200}
                    ,{field: 'updated_at', title: '更新时间',width:200}
                    ,{fixed: 'right', width: 200, align:'center', toolbar: '#options'}
                ]]
            });

            //监听工具条
            table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值
                if(layEvent === 'del'){
                    layer.confirm('确认删除吗？', function(index){
                        $.post("{{ route('admin.goods_category.destroy') }}",{_method:'delete',ids:data.id},function (result) {
                            if (result.code==0){
                                obj.del(); //删除对应行（tr）的DOM结构
                            }
                            layer.close(index);
                            layer.msg(result.msg)
                        });
                    });
                } else if(layEvent === 'edit'){
                    location.href = '/admin/goods_category_sku/'+data.id+'/edit';
                }
            });

            //搜索
            $("#searchBtn").click(function () {
                var catId = $("#category_id").val()
                dataTable.reload({
                    where:{category_id:catId},
                    page:{curr:1}
                })
            })
        })
    </script>
@endsection
